/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: agent-java
 * Description: Agent written in java language
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.agent.dao;

import com.google.common.collect.Maps;
import java.util.Map;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import ru.ffind.agent.db.DbFilename;
import ru.ffind.common.model.Filename;

/**
 * Implementation for updating files in batch.
 *
 * @author Rage Steel
 */
public class UpdateFileBatchImpl extends AbstractBatch 
        implements UpdateFileBatch {

    private SimpleJdbcTemplate simpleJdbcTemplate;

    public UpdateFileBatchImpl(SimpleJdbcTemplate simpleJdbcTemplate) {
        this.simpleJdbcTemplate = simpleJdbcTemplate;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updateFilename(DbFilename targetFile, Filename base) {
			Map<String, Object> fileUpdate = Maps.newHashMapWithExpectedSize(4);
			fileUpdate.put("file_id", targetFile.getFileId());
			fileUpdate.put("file_flags", targetFile.getFileFlags());
			fileUpdate.put("file_size", base.getFileSize());
			fileUpdate.put("file_datetime", base.getDatetime());
			fileArgs.add(fileUpdate);
    }

	private static final String SQL = "UPDATE `ff_files` " +
			"SET `file_flags` = :file_flags, `file_size` = :file_size, " +
            "`file_datetime` = :file_datetime " +
			"WHERE :file_id = `file_id`";

    /**
     * {@inheritDoc}
     */
    @Override
    protected void doExecute(Map<String, Object>[] batch) {
		simpleJdbcTemplate.batchUpdate(SQL, ArgUtils.toBatchArgs(fileArgs));
    }
}
